#include "detector.h"
#include "processimage.h"
#include "common/stringutils.h"
#include "common/symbol.h"

#include <QString>
#include <QSize>

namespace qrcode{

Detector::Detector()
{

}

void Detector::SimpleProcess(std::__cxx11::string imgfile)
{
    pixelImage.load(QString::fromStdString(imgfile));
    pixelImage.save("test.png");
    QSize size = pixelImage.size();
    StringUtils::PRINT("图像大小", size.height());
    ProcessImage *pProcess = new ProcessImage();

    //1.定位二维码图片，去除四周留白
    pProcess->RemoveBlank(pixelImage);

    //2.确定模块大小
    int mpix = ConfirmModulePixel(pixelImage);
    StringUtils::PRINT("模块大小：", mpix);

    //3.还原为1像素图像
    ConvertPixelImage(pixelImage, mpix);


}

QImage Detector::getPixelImage() const
{
    return pixelImage;
}

int Detector::ConfirmModulePixel(QImage &image)
{
    QSize size = image.size();
    int i = 0;
    while (i < size.width()) {
        if(image.pixelColor(i, i) == value_w){
            break;
        }
        i++;
    }
    return i;
}

void Detector::ConvertPixelImage(QImage &image, int mpix)
{
    QSize size = image.size();
    int mods = size.width() / mpix;
    QImage img(mods, mods, QImage::Format_RGB32);

    for(int i = 0; i < mods; i++){
        for(int j = 0; j < mods; j++){
            img.setPixelColor(i, j, image.pixelColor(mpix*i, mpix*j));
        }
    }
    image = img;
}

}
